package ControlLayer;

import ModelLayer.*;

/**
 * Write a description of class LoginController here.
 * 
 * @author Group1
 * @version 2011-06-08
 */
public class LoginController
{
    private LoginContainer loginContainer;
    
    /**
     * Add the excisting login
     */
    public LoginController()
    {
        loginContainer = LoginContainer.getInstance();
        addAlreadyExcistingLogins();
    }
    
    /**
     * Creates a Login
     * @param loginName LoginName of Login
     * @param password Password of Login
     * @param throws Exception Throws an error further up in the system
     */
    public void createLogin(String loginName, String password) throws Exception
    {
        if(loginContainer.searchForLoginName(loginName) != null)
        {
            throw new Exception("Loginname already exist");
        }
        else
        {
            Login newLogin = new Login(loginName, password);
            loginContainer.createLogin(newLogin);
        }
    }
    
    /**
     * Creates 3 Login in loginContainer and add them in the LoginController
     * @param take a parameter of type Login
     */
    public void addAlreadyExcistingLogins()
    {
        Login newLogin1 = new Login("manager", "123456");
        Login newLogin2 = new Login("salesman", "654321");
        Login newLogin3 = new Login("worker", "123123");
        
        loginContainer.createLogin(newLogin1);
        loginContainer.createLogin(newLogin2);
        loginContainer.createLogin(newLogin3);
    }
    
    /**
     * Delete a Login
     * @param throws Exception Throws an error further up in the system
     * @param loginName LoginName of the Login
     */
    public void deleteLogin(String loginName) throws Exception
    {
        if(loginContainer.searchForLoginName(loginName) == null)
        {
            throw new Exception("Login name not found");
        }
        else
        {
            loginContainer.deleteLogin(loginName);
        }
    }
    
    /**
     * Check if the loginName and password are false if yes throws an error futher up in the system, and if not return true
     * @param loginName LoginName of the Login
     * @param password Password of the Login
     */
    public boolean checkLoginNameAndPassword(String loginName, String password) throws Exception
    {
        if(loginContainer.searchForLogin(loginName, password) == false)
        {
            throw new Exception("Error - Wrong loginname or password");
        }
        else
        {
            return true;
        }
    }
    
}
